package com.team.kaidb.presenter.impl;

import android.content.Context;
import android.text.TextUtils;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.team.kaidb.bean.request.IRequestBean;
import com.team.kaidb.bean.request.LoginRequestBean;
import com.team.kaidb.bean.response.LoginResponseBean;
import com.team.kaidb.model.IModelLoginData;
import com.team.kaidb.model.impl.ModelLoginDataImpl;
import com.team.kaidb.presenter.LoginPresenter;
import com.team.kaidb.presenter.OnLoginDataListener;
import com.team.kaidb.ui.IViewLogin;
import com.team.kaidb.utils.L;

/**
 * Created by maizi on 15/6/27.
 * 登陆的Presenter
 */
public class LoginPresenterImpl implements OnLoginDataListener,LoginPresenter{

    private IViewLogin viewActLogin;
    private IModelLoginData modelLoginData;

    public LoginPresenterImpl(IViewLogin viewActLogin) {
        this.viewActLogin = viewActLogin;
        modelLoginData = new ModelLoginDataImpl();
    }


    @Override
    public void onLoadSuccess(String result) {
        L.wj("result"+result);
        //这里应该将result的转化为JsonObject对象
        Gson gson = new Gson();
        LoginResponseBean bean = gson.fromJson(result, LoginResponseBean.class);
        if (bean != null&&!TextUtils.isEmpty(bean.message))
            viewActLogin.loginSuccess(""+bean.message);
        else
            viewActLogin.loginFaild("登录失败");
    }

    @Override
    public void onLoadFaild(VolleyError error) {
        viewActLogin.loginFaild("登录失败");
    }

    @Override
    public void login(IRequestBean bean,Context context) {
        modelLoginData.getLoginData(bean,this);
    }

}
